﻿using AutoMapper;
using NetTaste;
 
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using VM.Common.Result;
using VM.Entity.EntityDto.Order;
using VM.Entity.EntityMap.Goods;
using VM.Entity.EntityMap.ICon;
using VM.Entity.EntityMap.Lunbo;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.Order;
using VM.Entity.EntityMap.People;
using VM.Entity.EntityMap.ShopCar;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.Entity.EntityMap.User;
using VM.IDAL.ShopCar;

namespace VM.DAL.ShopCar
{
    public class OrderDAL : BaseDAL, IOrderDAL
    {

        protected ISqlSugarClient _Client { get; set; }
        protected IMapper _IMapper { get; set; }

        public OrderDAL(ISqlSugarClient client, IMapper iMapper) : base(client)
        {
            _Client = client;
            _IMapper = iMapper;
        }
 
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public sys_order addOrder(sys_order obj)
        {

            sys_order Res = _Client.InsertNav(obj)
                                 .Include(a=>a.orderPeople)
                                  //.ExecuteCommand();
                                  .ExecuteReturnEntity();

             

                    if (Res.id>0)
                    {
                        return Res;
                    }
                    else
                    {
                        return null;
                    }
        }
 




        /// <summary>
        /// 获取商品列表，根据商品类别
        /// </summary>
        /// <param name="goodsTypeId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PagingData<getOrderListDto> getOrderList(int userId ,int status ,string? OrderInfo="", int pageIndex=1, int pageSize=100)
        {
            var listRes =
                 _Client.Queryable<sys_order>()//.ToList();
                 .Includes(a=>a.good)
                 .Includes(a => a.good.headImgs)
                .Includes(a => a.spec)
                .Includes(a => a.createUser)
                //查询二层两层
                .Includes(a=>a.orderPeople, b => b.linkUser)
                .Where(a => a.createUserId == userId)
                .Where(a => a.status != 999)
                .WhereIF(status != 0, a => a.status == status)
                //模糊查询
               .WhereIF(OrderInfo != null && OrderInfo!="" && OrderInfo != "0", it => it.name.Contains(OrderInfo))
                .OrderByDescending(a => a.id)
                ;

            PagingData<sys_order> result = new PagingData<sys_order>()
            {
                dataList = listRes.ToPageList(pageIndex, pageSize),
                pageIndex = pageIndex,
                pageSize = pageSize,
                recordCount = listRes.Count(),
                searchString = ""
            };
            PagingData<getOrderListDto> resend = _IMapper.Map<PagingData<sys_order>, PagingData<getOrderListDto>>(result);

            return resend;
        }


        public int deleteOrderById(int userId, int shopCarId)
        {
            //bool     listRes =
            //      _Client.DeleteNav<sys_order>(it => it.id == shopCarId)
            //             .Include(a => a.orderPeople)
            //             .ExecuteCommand();

            sys_order peopleInfo = new sys_order()
            {
                id = shopCarId,
                status = 999
            };
            int result = _Client.Updateable(peopleInfo)
                .UpdateColumns(it => new { it.status })
                .IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true)
                .ExecuteCommand();
            return result;




            
           
        }

        public getOrderInfoDto getOrderInfoById(int id)
        {
            List<sys_order> listRes =
                 _Client.Queryable<sys_order>()//.ToList();
                 .Includes(a => a.good)
                 .Includes(a => a.good.headImgs)
                 .Includes(a => a.spec)
                 .Includes(a => a.createUser)
                //查询二层两层
                      .Includes(a => a.orderPeople, b => b.linkUser)
                      .Where(a => a.id == id).ToList()  
                ;
            getOrderInfoDto res= _IMapper.Map<sys_order, getOrderInfoDto>(listRes[0]);
            return res;
        }

        public int cancelOrderById(int id)
        {
            sys_order peopleInfo = new sys_order()
            {
                id = id,
                status = 4
            };
            int result = _Client.Updateable(peopleInfo)
                .UpdateColumns(it => new { it.status })
                .IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true)
                .ExecuteCommand();
            return result;
        }

        public int updateOrderStatusById(updateOrderStatusByIdDto item)
        {

            sys_order peopleInfo = new sys_order()
            {
                id = (int)item.id,
                status = item.status
            };
            int result = _Client.Updateable(peopleInfo)
                .UpdateColumns(it => new { it.status })
                .IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true)
                .ExecuteCommand();
            return result;

        }

        public int updateOrderQty( int specId, int qty)
        {
        
            int result = _Client.Updateable<good_spec>()
                             .SetColumns(it => it.qty == it.qty + qty)
                             .Where(it => it.id == specId)
                             .ExecuteCommand();
            
            return result;
        }

        public int checkStockQty(int specId, int qty)
        {
            List<good_spec> listRes =
                _Client.Queryable<good_spec>()
                 .Where(a => a.id == specId).ToList()
               ;
            int stockQty= listRes[0].qty;
            int ResQty = stockQty - qty;
            return ResQty;
           
        }
    }
}
